package br.edu.sistemaacademico.util;

import java.util.List;

import br.edu.sistemaacademico.iterator.Iterator;

public class Vetor {
   private static final int DEFAULT_SIZE = 250;

   private Object[] valores;
   private int      total_geral, total_atual;
   private int      atual;

   public Vetor() { this(DEFAULT_SIZE); }
   public Vetor(int size) {
      atual       = 0;
      total_atual = 0;
      total_geral = size;
      valores     = new Object[total_geral];
   } // public Vetor()
   
   public Vetor(List<Object> list) {
	      atual       = 0;
	      total_atual = 0;
	      total_geral = list.size()+1;
	      valores     = new Object[total_geral];
	   for(int i=0;i< list.size();i++){
		      valores[atual++] = list.get(i);
		      ++total_atual;
	   }
   }

   public void add(Object obj) {
      if (atual >= total_geral) {
         total_geral += DEFAULT_SIZE;
         Object[] temp = new Object[total_geral];
         System.arraycopy(valores,0,temp,0,valores.length);
         valores = temp;
      } 
      valores[atual++] = obj;
      ++total_atual;
   } 
   
   public void add(List<Object> list){
	   for(int i=0;i< list.size();i++){
		   if (atual >= total_geral) {
		         total_geral += DEFAULT_SIZE;
		         Object[] temp = new Object[total_geral];
		         System.arraycopy(valores,0,temp,0,valores.length);
		         valores = temp;
		      } 		   
		      valores[atual++] = list.get(i);
		      ++total_atual;
	   }
   }
   
   public Iterator createIterator() { return new VetorIterator(); }

   private class VetorIterator implements Iterator {
      private int indice = Integer.MAX_VALUE;

      @Override
      public Object first() { indice = 0; return next(); }

      @Override
      public Object next() { return valores[indice++]; }

      @Override
      public boolean isDone() { return indice > total_atual; }
   } 
}
